package com.daocao.jy.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.jy.R;
import com.daocao.jy.activity.MyOrderDetailActivity;
import com.daocao.jy.api.model.MyOrderListModel;
import com.daocao.jy.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOrderAdapter extends ItemAdapter<MyOrderListModel.DatasBean, MyOrderAdapter.DefaultHolder> {
    private final int textSize28;
    private final int textSize24;
    private final int textSize32;
    private final int textSize22;
    private Context context;

    public MyOrderAdapter(Context context) {
        super(context);
        textSize28 = context.getResources().getDimensionPixelSize(R.dimen.x28);
        textSize24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        textSize32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        textSize22 = context.getResources().getDimensionPixelSize(R.dimen.x22);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        MyOrderListModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.txtTitle.setText(item.name);
        String strsub = "";
        if (item.card_number.length() >= 4) {// 判断是否长度大于等于4
            strsub = item.card_number.substring(item.card_number.length() - 4);//一个参数表示截取传递的序号之后的部分
        } else {
            strsub = item.card_number;
        }
        Spanned cardType = Spans.with(context).font(item.card_type, textSize28, 0xff333333).
                font("(卡尾号" + strsub + ")", textSize24, 0xffFB5E01).
                build();
        holder.txtCardType.setText(cardType);
        //订单状态（-2：失效，-1：失败，0：待支付，1：完成，2：已付款，3：分期中）
        holder.txtStatus.setText(getStatus(item.status));

        Spanned money = Spans.with(context).font("￥" + item.pay_money, textSize32, 0xffFB5E01).style(Typeface.BOLD).
                font(" 原价", textSize22, 0xff999999).
                font("￥" + item.money, textSize22, 0xff999999).centerline().
                build();
        holder.txtContent.setText(money);
        if (item.status.equals("0")) {
            holder.btnBuy.setVisibility(View.VISIBLE);
        } else {
            holder.btnBuy.setVisibility(View.GONE);
        }
        if (item.card_type.equals("中国石油")) {
            holder.imgCardType.setImageResource(R.mipmap.img_type_shiyou);
        } else if (item.card_type.equals("中国石化")) {
            holder.imgCardType.setImageResource(R.mipmap.img_type_shihua);
        }
    }

    private String getStatus(String status) {
        switch (status) {
            case "-2":
                return "失效";
            case "-1":
                return "失败";
            case "0":
                return "待支付";
            case "1":
                return "完成";
            case "2":
                return "已付款";
            case "3":
                return "分期中";
            default:
                return "";
        }
    }


    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.txt_card_type)
        TextView txtCardType;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.img_card_type)
        ImageView imgCardType;
        @BindView(R.id.fl_card_type)
        FrameLayout flCardType;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.btn_buy)
        SuperButton btnBuy;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        MyOrderListModel.DatasBean item;

        @OnClick({R.id.btn_buy, R.id.layout_total})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_buy:
                    if (item.status.equals("0")) {
                        if (onChooseListener != null) {
                            onChooseListener.onClick(item.pay_money, item.order_no);
                        }
                    }
                    break;
                case R.id.layout_total:
                    Intent intent = new Intent(context, MyOrderDetailActivity.class);
                    intent.putExtra("order_number", item.order_no);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String money, String order_no);
    }
}
